﻿using System;

namespace OpenWaves.ImageTransformations
{
    public class CentralCropTransformation : ImageTransformation
    {
        public CentralCropTransformation(int width, int height) : base(width, height)
        {
        }

        public CentralCropTransformation(string serializedProperties) : base(serializedProperties)
        {
        }

        public override void ApplyToImage(IImage image)
        {
            if (image.Width <= this.Width && image.Height <= this.Height)
                return;

            image.Crop(
                Math.Max(0, image.Width - this.Width)/2, 
                Math.Max(0, image.Height - this.Height)/2, 
                Math.Min(image.Width, this.Width), 
                Math.Min(image.Height, this.Height));
        }

        protected override IImageTransformation Scale(int width, int height)
        {
            return new CentralCropTransformation(width, height);
        }
    }
}